package com.engr.web.form;

import com.engr.annotation.FreeAccess;
import com.engr.base.BaseController;
import com.engr.core.service.from.FormIndexService;
import com.engr.domain.FormIndex;
import com.engr.util.ResponseMessage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: train-project
 * @description: 公共资源模块
 * @author: lyx
 * @create: 2018-10-06
 **/
@RestController
@RequestMapping(value = "/api/form",produces = { MediaType.APPLICATION_JSON_UTF8_VALUE,
        MediaType.APPLICATION_JSON_VALUE } )
public class FormRestController extends BaseController {

    @Autowired
    private FormIndexService formIndexService;


    @FreeAccess
    @GetMapping(value = "/index",name = "查询目录索引")
    @ApiOperation(value = "目录索引",notes = "目录索引")
    public ResponseMessage findAllIndex(){

        /*List<FormIndex> result = formIndexService.findAllIndex();
        if(result.isEmpty()){
            ResponseMessage.error("未查询到结果");
        }*/
        System.out.println("hello world");
        return ResponseMessage.success();
    }


    @GetMapping(value = "/page",name = "查询目录索引")
    @ApiOperation(value = "分页目录索引",notes = "分页目录索引")
    public ResponseMessage findAllByPage(@ApiParam(value = "每页记录数", allowableValues = "2,5,10,15,20,25,30") @RequestParam(defaultValue = "10") int pageSize,
                                         @ApiParam(value = "当前页码数(从1开始)") @RequestParam(defaultValue = "1") int pageIndex){

        Page<FormIndex> result = formIndexService.findAllByPage(PageRequest.of(pageIndex-1,pageSize));
        if(result.getContent().isEmpty()){
            ResponseMessage.error("未查询到结果");
        }
        return ResponseMessage.success(result.getContent());
    }

}
